package DEEPERsource.DEEPERsource.source.machinelearning.kernel;

import weka.core.Instance;
import machinelearning.features.FeatureBuilder;
import machinelearning.features.POSFeatureBuilder;
import machinelearning.features.ParseFeatureBuilder;
import deeper.Interaction;

public class CompositionKernel implements Kernel<Interaction> {
	
	Kernel<Interaction> syntacticKernel = new SyntacticKernel();
	Kernel<double[]> polyKernel = new PolynomialKernel();
	
	public CompositionKernel(){
		((PolynomialKernel)polyKernel).degree = 2;
	}

	public double compute(Interaction i1, Interaction i2) {
		double result = 0;
		if(i1.depPath == null || i2.depPath == null){
			return result;
		}
		double treeKernel = syntacticKernel.compute(i1, i2);
		//FeatureBuilder fb = new POSFeatureBuilder(true);
		/*FeatureBuilder fb = new ParseFeatureBuilder(false);
		Instance inst1 = fb.getFeatureVector(i1);
		double[] vector1 = inst1.toDoubleArray();
		Instance inst2 = fb.getFeatureVector(i2);
		double[] vector2 = inst2.toDoubleArray();
		((PolynomialKernel)polyKernel).gamma = 1.0/vector1.length;
		double value = polyKernel.compute(vector1, vector2);
		*/
		//if(i1.depPath.length>6 || i2.depPath.length>6)
		//	return 0;
		/*double len = (double)Math.abs(i1.depPath.length - i2.depPath.length);
		if(len==0)
			len=1;		
		return treeKernel/Math.pow(len, 2);
		*/
		if(treeKernel==0)
			return 0;
		return Math.log(treeKernel);
	}

}
